/**
 * 工具包
 * */
layui.define((exports) => {
	let sdUtils = {
		time(_type) {
			let type = 0;
			let t = "";
			if (_type) {
				type = _type;
			}
			var myDate = new Date();
			//获取当前年
			var year = myDate.getFullYear();
			//获取当前月
			var month = myDate.getMonth() + 1;
			//获取当前日
			var date = myDate.getDate();
			var h = myDate.getHours(); //获取当前小时数(0-23)
			var m = myDate.getMinutes(); //获取当前分钟数(0-59)
			var s = myDate.getSeconds();

			switch (type) {
				case 1:
					t = year + '-' + utils.string.time(month);
					break;
				case 2:
					t = year + '-' + utils.string.time(month) + "-" + utils.string.time(date);
					break;
				case 3:
					t = year + '-' + utils.string.time(month) + "-" + utils.string.time(date) + " " +
						utils.string.time(h) + ':' + utils.string.time(m);
					break;
				default:
					t = year + '-' + utils.string.time(month) + "-" + utils.string.time(date) + " " +
						utils.string.time(h) + ':' + utils.string.time(m) + ":" + utils.string.time(s);
					break;
			}
			return t;
		},
		// 字符串处理
		string: {
			// 生成uudi
			guid() {
				return 'xxxxxxxx-xxxx-txxx-jxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
					let r = Math.random() * 16 | 0,
						v = c == 'x' ? r : (r & 0x3 | 0x8);
					return v.toString(16);
				});
			},
			// 补全0(>10)
			time(_t) {
				let t = parseInt(_t);
				if (t < 10) {
					t = '0' + t;
				}
				return t;
			},
			formatCode(_txt) {
				let code = 0;
				// 1text 2number 3date 4textarea 5radio 6multiple 7edit 8image 9images 10file 11password
				switch (_txt) {
					case 'text':
						code = 1;
						break;
					case 'number':
						code = 2;
						break;
					case 'date':
						code = 3;
						break;
					case 'textarea':
						code = 4;
						break;
					case 'radio':
						code = 5;
						break;
					case 'multiple':
						code = 6;
						break;
					case 'edit':
						code = 7;
						break;
					case 'image':
						code = 8;
						break;
					case 'images':
						code = 9;
						break;
					case 'file':
						code = 10;
						break;
					case 'password':
						code = 11;
						break;
				}
				return code;
			}
		},
		// 转换
		convert: {
			// 字符串转数字
			s2n(_s) {
				let n = 0;
				if (!isNaN(parseInt(_s))) {
					n = parseInt(_s);
				}
				return n;
			},
			// 时间戳转日期
			t2t(_timestamp) {
				_timestamp = (typeof _timestamp) == 'string' ? Number(_timestamp) : _timestamp;
				if (_timestamp > 0) {
					let d = null;
					if ((_timestamp.toString()).length == 13) {
						d = new Date(_timestamp);

					} else {
						d = new Date(_timestamp * 1000);
					}

					let date = (d.getFullYear()) + "-" +
						utils.string.time((d.getMonth() + 1)) + "-" +
						utils.string.time(d.getDate()) + " " +
						utils.string.time(d.getHours()) + ":" +
						utils.string.time(d.getMinutes()) + ":" +
						utils.string.time(d.getSeconds());
					return date;
				} else {
					return '';
				}
			},
			t2s(_timestamp) {
				_timestamp = (typeof _timestamp) == 'string' ? Number(_timestamp) : _timestamp;
				if (_timestamp > 0) {
					let d = null;
					if ((_timestamp.toString()).length == 13) {
						d = new Date(_timestamp);

					} else {
						d = new Date(_timestamp * 1000);
					}

					let date = (d.getFullYear()) + "-" +
						utils.string.time((d.getMonth() + 1)) + "-" +
						utils.string.time(d.getDate());
					return date;
				} else {
					return '';
				}
			},
			t2m(_timestamp) {
				_timestamp = (typeof _timestamp) == 'string' ? Number(_timestamp) : _timestamp;
				if (_timestamp > 0) {
					let d = null;
					if ((_timestamp.toString()).length == 13) {
						d = new Date(_timestamp);

					} else {
						d = new Date(_timestamp * 1000);
					}

					let date = (d.getFullYear()) + "-" +
						utils.string.time((d.getMonth() + 1));
					return date;
				} else {
					return '';
				}
			},
			t2c(_timestamp) {
				_timestamp = (typeof _timestamp) == 'string' ? Number(_timestamp) : _timestamp;
				if (_timestamp > 0) {
					let d = null;
					if ((_timestamp.toString()).length == 13) {
						d = new Date(_timestamp);

					} else {
						d = new Date(_timestamp * 1000);
					}

					let date = (d.getFullYear()) + "-" +
						utils.string.time((d.getMonth() + 1)) + "-" +
						utils.string.time(d.getDate()) + " " +
						utils.string.time(d.getHours()) + ":" +
						utils.string.time(d.getMinutes());
					return date;
				} else {
					return '';
				}
			},
			d2t(date) {
				let timestamp = Date.parse(new Date(date));
				return timestamp / 1000;
			}
		},
		// 参数读取
		url: {
			js(id) {
				id = (id.indexOf('#') >= 0) ? id : ('#' + id);
				let item = $(id).attr('src').split('?');
				if (item.length > 1) {
					let params = item[1].split("&");
					let code = {};
					$.each(params, function(i, param) {
						let item = param.split('=');
						code[item[0]] = item[1];
					})
					return code;
				} else {
					return null;
				}
			},
			param(key) {
				const url = location.href; //获取url中"?"符后的字串
				let theRequest = new Object();
				if (url.indexOf("?") != -1) {
					let str = url.split("?");
					let strs = str[1].split("&");
					for (let i = 0; i < strs.length; i++) {
						theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
					}
				}
				return theRequest[key];
			}
		},
		// 动态加载js
		script: function(callback) {
			let js = document.createElement("script");
			this.js = js;
			js.type = "text/javascript";
			document.body.appendChild(js);

			sdUtils.script.prototype.set = function(url, id) {
				// 需要判断js是否已经存在
				this.js.src = url;
				id && (this.js.id = id);
			};
			if (callback) {
				if (navigator.appName.toLowerCase().indexOf('netscape') === -1) {
					js.onreadystatechange = function() {
						js.readyState === 'complete' && callback(this);
					}
				} else {
					js.onload = function() {
						callback(this);
					}
				}
			}
		},
		file: {
			// 随机生成一个文件名
			createFileName(file) {
				let fix = (file.name).split('.')[1];
				let name = sdUtils.string.guid() + '.' + fix;
				let type = (file.type).split('/')[0];
				return {
					size: file.size / 1024,
					original: file.name,
					name: name,
					type: type
				};
			},
		},
		// oss处理
		oss: {
			// 路径补全
			format(name, param) {
				let full = 'https://oss.xtaller.com/' + name;
				full = param ? full + '?' + param : full;
				return full;
			},
			// 图片预处理
			style(type) {
				type = type ? type : 1;
				let name = '';
				switch (type) {
					case 1:
						name = 'admin-edit-image';
						break;
					case 2:
						name = 'img-baby-mobile';
						break;
					case 3:
						name = 'img-baby-pc';
						break;
					case 4:
						name = 'img-baby-mobile-big';
						break;
					case 5:
						name = 'img-baby-min-preview';
						break;
					case 6:
						name = 'mini-photo';
						break;
					case 7:
						name = 'img-user-header';
						break;
				}
				return '?x-oss-process=' + name;
			}
		},
	}
	exports('sdUtils', sdUtils);
})
